﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BadHabit.Factory.Controls {

    /// <summary>
    /// Show the project's summary information.
    /// </summary>
    public partial class SummaryPanel : GenericPanel {

        
       
        
        /// <summary>
        /// Default Constructor.
        /// </summary>
        public SummaryPanel() {
            InitializeComponent();                        
        }



        private void UpdateProjectIndicators() {

            double qualityIndicatorValue = 0.0;
            double velocityIndicatorValue = 0.0;
            double budgetIndicatorValue = 0.0;
            double deadlineIndicatorValue = 0.0;
            double incomeIndicatorValue = 0.0;
            double sizeIndicatorValue = 0.0;
            double walkedIndicatorValue = 0.0;
            

            try {
                Client.Service.RetriveProjectIndicators(Project.ProjectUId,
                    ref budgetIndicatorValue,                    
                    ref qualityIndicatorValue,
                    ref velocityIndicatorValue,
                    ref deadlineIndicatorValue,
                    ref sizeIndicatorValue,
                    ref incomeIndicatorValue,
                    ref walkedIndicatorValue);
            } catch (Exception ex) {
                Windows.Error.ShowError(ex, Properties.Resources.COULD_NOT_READ_PROJECT_INDICATORS);
                return;
            }

            budgetIndicator.IndicatorValue = budgetIndicatorValue;
            deadlineIndicator.IndicatorValue = deadlineIndicatorValue;
            qualityIndicator.IndicatorValue = qualityIndicatorValue;
            velocityIndicator.IndicatorValue = velocityIndicatorValue;
            walkedIndicator.IndicatorValue = walkedIndicatorValue;

            budgetIndicator.IsIndicatorGood = (budgetIndicator.IndicatorValue < 100.0);
            qualityIndicator.IsIndicatorGood = (qualityIndicator.IndicatorValue < 10.0);
            deadlineIndicator.IsIndicatorGood = (deadlineIndicator.IndicatorValue >= 0.0);
            velocityIndicator.IsIndicatorGood = (velocityIndicator.IndicatorValue >= Client.TeamVelocity);
            walkedIndicator.IsIndicatorGood = (walkedIndicatorValue > 0);

            sizeIndicator.Rank = (int)sizeIndicatorValue;
            incomeIndicator.Rank = (int)incomeIndicatorValue;

        }

        

       
        
        /// <summary>
        /// Verify if the user can start the project
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StartProject_CanExecute(object sender, CanExecuteRoutedEventArgs e) {
            if (Project == null)
                return;
            bool projectStatus = (Project.Status == (short)ProjectStatus.PROPOSAL_CREATION ||Project.Status == (short)ProjectStatus.PROPOSAL_APPROVED || Project.Status == (short)ProjectStatus.PROPOSAL_WAITING);
            e.CanExecute = projectStatus;
        }

        /// <summary>
        /// Starts the project.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StartProject_Executed(object sender, ExecutedRoutedEventArgs e) {


            if (Project.LastSprint.ExpirationDate < DateTime.Today) {
                Windows.Error.ShowAlert(Properties.Resources.Can_not_start_project_because_all_sprints_are_over);
                return;
            }

            MessageBoxResult d = Windows.Error.ShowDialog(
                Properties.Resources.Confirm_start_project,
                Properties.Resources.Starting_Project, MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
            if (d == MessageBoxResult.No)
                return;

            try {
                Project.ValidateToSave();
            } catch (Exception ex) {
                BadHabit.Factory.Windows.Error.ShowAlert(ex.Message);
                return;
            }

            

            if(Project.Baseline==0 && Project.HourBankMode==0)
                Project.CreateNewBaseLine();

            Project.Status = (short) ProjectStatus.PROJECT_STARTED;
            Project.StartDate = DateTime.Now;

            Client.FactoryMainWindow.Save();

            

            
        }

        /// <summary>
        /// Verify if the user can cancel the project.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelProject_CanExecute(object sender, CanExecuteRoutedEventArgs e) {
            if (Project == null)
                return;         
            bool projectStatus = (Project.Status != (short)ProjectStatus.PROJECT_CANCELED
                                && Project.Status != (short)ProjectStatus.PROPOSAL_REJECTED
                                && Project.Status != (short)ProjectStatus.PROJECT_DONE
                                && Project.Status != (short)ProjectStatus.CREATION
                                && Project.Status != (short)ProjectStatus.PROJECT_SUSPENDED);
            e.CanExecute = projectStatus;
        }

        /// <summary>
        /// Cancel the project.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelProject_Executed(object sender, ExecutedRoutedEventArgs e) {
            MessageBoxResult d = Windows.Error.ShowDialog(
                Properties.Resources.Confirm_cancel_project, Properties.Resources.Canceling_Project, MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
            if (d == MessageBoxResult.No)
                return;
            Project.EndDate = DateTime.Now;
            Project.Status = (short)ProjectStatus.PROJECT_CANCELED;

            Client.FactoryMainWindow.Save();
        }

        /// <summary>
        /// Verify if the user can close the project.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CloseProject_CanExecute(object sender, CanExecuteRoutedEventArgs e) {
            if (Project == null)
                return;
          
            bool projectStatus = (Project.Status == (short)ProjectStatus.PROJECT_MAINTANCE
                                || Project.Status == (short)ProjectStatus.PROJECT_TESTFASE                                
                                || Project.Status == (short)ProjectStatus.PROPOSAL_APPROVED
                                || Project.Status == (short)ProjectStatus.PROJECT_STARTED);
                                
            e.CanExecute = projectStatus;
        }

        /// <summary>
        /// Close the project.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CloseProject_Executed(object sender, ExecutedRoutedEventArgs e) {
            MessageBoxResult d = Windows.Error.ShowDialog(
                Properties.Resources.Confirm_close_project, Properties.Resources.Closing_Project,
                MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
            if (d == MessageBoxResult.No)
                return;


            if (Project.IsThereAnyUnfinishItem) {
                MessageBoxResult d2 = Windows.Error.ShowDialog(
                    Properties.Resources.Confirm_finish_backlog_items, Properties.Resources.Closing_Project, MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.Yes);
                if (d2 == MessageBoxResult.Yes)
                    Project.FinishItemsToClose();
            }

            Project.EndDate = DateTime.Now;
            Project.Status = (short)ProjectStatus.PROJECT_DONE;

            Client.FactoryMainWindow.Save();
        }

      
        

        

        private void summaryPanel_Loaded(object sender, RoutedEventArgs e) {
            projectNameTextBox.Focus();
        }

        private void changeParent_Click(object sender, RoutedEventArgs e) {
            changeParentButton.Visibility = Visibility.Collapsed;
            parentProjectUIdComboBox.Visibility = Visibility.Visible;

            Data.ProjectDataSet dsParentProjects = new BadHabit.Factory.Data.ProjectDataSet();
            try {
                dsParentProjects.Merge(Client.Service.RetriveAllPayableProjects());
            } catch (Exception ex) {
                Windows.Error.ShowError(ex, Properties.Resources.COULD_NOT_READ_PARENT_PROJECTS);
                changeParentButton.Visibility = Visibility.Visible;
                parentProjectUIdComboBox.Visibility = Visibility.Collapsed;
                return;
            }

            parentProjectUIdComboBox.ItemsSource = dsParentProjects.Project.Select("", "ClientName, ProjectName, ProjectNumber");
            

        }

        private void CloseParentProjectDropDown() {
            if(Project.HourBankMode>0)
                changeParentButton.Visibility = Visibility.Visible;
            parentProjectUIdComboBox.Visibility = Visibility.Collapsed;
        }

        private void parentProjectUIdComboBox_DropDownClosed(object sender, EventArgs e) {
            CloseParentProjectDropDown();
        }

  

        private void summaryPanel_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e) {
            DataContextAsProjectDataSet.Project.ColumnChanging += new System.Data.DataColumnChangeEventHandler(Project_ColumnChanging);
            CloseParentProjectDropDown();

            
        }

        void Project_ColumnChanging(object sender, System.Data.DataColumnChangeEventArgs e) {
            // if the sprint expiration date was changed
            if (e.Column == DataContextAsProjectDataSet.Project.EstimatedStartDateColumn) {                
                Client.ShiftSprints(Project, (DateTime)e.ProposedValue);
            }
        }


        /// <summary>
        /// This should overwrited.
        /// </summary>
        /// <returns></returns>
        protected override bool RequestData() {
            UpdateProjectIndicators();
            return true;
        }


        /// <summary>
        /// Verify if the user can close the project.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SuspendProject_CanExecute(object sender, CanExecuteRoutedEventArgs e) {
            if (Project == null)
                return;

            bool projectStatus = (Project.Status != (short)ProjectStatus.PROJECT_DONE
                && Project.Status != (short)ProjectStatus.PROPOSAL_REJECTED
                && Project.Status != (short)ProjectStatus.PROJECT_CANCELED);
                                

            e.CanExecute = projectStatus;
        }

        private void Hyperlink_Click(object sender, RoutedEventArgs e) {
            Client.FactoryMainWindow.Refresh(DataContextAsProjectDataSet.Project[0].PaymentProjectUId);
        }

        private void clientComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            //Client.FactoryMainWindow.tabControl.teamPanel.LoadClientContacts(clientComboBox.SelectedItem.ToString());
        }

        

       
    }
}
